package cn.fay.chapter17;

import java.util.WeakHashMap;

public class CanonicalMapping {
	public static void main(String[] args) {
		int size=1000;
		Key[] keys=new Key[size];
		WeakHashMap<Key, Value> map=new WeakHashMap<Key, Value>();
		for(int i=0;i<size;i++){
			Key k=new Key(Integer.toString(i));
			Value value=new Value(Integer.toString(i));
			if (i%3==0) {
				keys[i]=k;
			}
			map.put(k, value);
		}
		System.gc();
	}
}
class Element{
	private String ident;
	public Element(String id){ident=id;}
	@Override
	public String toString() {
		return ident;
	}
	@Override
	public int hashCode() {
		return ident.hashCode();
	}
	@Override
	public boolean equals(Object obj) {
		return obj instanceof Element && ident.equals(((Element)obj).ident);
	}
	@Override
	protected void finalize() throws Throwable {
		System.out.println("Finalize "+getClass().getSimpleName()+" "+ident);
	}
}
class Key extends Element{
	public Key(String id) {
		super(id);
	}
}
class Value extends Element{
	public Value(String id) {
		super(id);
	}
}
